package com.kingyea.mobilepolice.apimanage.accesscontrol.service.impl;

import com.alibaba.fastjson.JSON;
import com.kingyea.mobilepolice.apimanage.accesscontrol.bean.AccessController;
import com.kingyea.mobilepolice.apimanage.accesscontrol.dao.AccessControllerMapper;
import com.kingyea.mobilepolice.apimanage.accesscontrol.handle.Keyword;
import com.kingyea.mobilepolice.apimanage.accesscontrol.service.AccessControllerService;
import com.kingyea.mobilepolice.apimanage.dictionary.service.DictionaryService;
import com.kingyea.mobilepolice.apimanage.paraminfo.bean.ParamInfo;
import com.kingyea.mobilepolice.apimanage.paraminfo.dao.ParamInfoMapper;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import com.kingyea.mobilepolice.dictionary.bean.Dictionary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chenjw
 * @version v1.0.0
 * @description AccessController 实现类
 * @date Created in 2018-04-03 15:31:23
 */
@Service
@Transactional
public class AccessControllerServiceImpl implements AccessControllerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccessControllerServiceImpl.class);
    @Autowired
    DictionaryService dictionaryService;
    @Resource
    private AccessControllerMapper accessControllerMapper;
    @Resource
    private ParamInfoMapper paramInfoMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public AccessController getById(Integer id) throws Exception {
        return this.accessControllerMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<AccessController> list(AccessController accessController) throws Exception {
        List<AccessController> list = accessControllerMapper.list(accessController);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer id) throws Exception {
        accessControllerMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param accessController
     * @return
     */
    @Send2Queue
    @Override
    public AccessController save(AccessController accessController) throws Exception {
        try {
            accessControllerMapper.save(accessController);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessController;
    }

    /**
     * 更新
     *
     * @param accessController
     * @return
     */
    @Send2Queue
    @Override
    public AccessController update(AccessController accessController) throws Exception {
        try {
            accessControllerMapper.update(accessController);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessController;
    }

    @Override
    public List<Keyword> getKeyworList(String serviceId) {
        return accessControllerMapper.getKeywordList(serviceId);
    }

    @Override
    public AccessController getByServiceId(Integer serviceId) {
        return accessControllerMapper.getByServiceId(serviceId);
    }

    @Override
    public PageInfo searchByCondition(Map<String, Object> map) throws Exception {
        String mapKey_policeType = "policeType";
        if (map.containsKey(mapKey_policeType) && !StringUtils.isEmpty(map.get(mapKey_policeType))) {
            Integer[] policeType = JSON.parseObject(JSON.toJSONString(map.get(mapKey_policeType)), Integer[].class);
            List<Integer> querySub = new ArrayList<>();
            for (Integer integer : policeType) {
                List<Dictionary> subList = dictionaryService.getSubNodeListByPid(new HashMap<String, Integer>() {{
                    put("pid", integer);
                }});
                subList = JSON.parseArray(JSON.toJSONString(subList), Dictionary.class);
                for (Dictionary dictionary : subList) {
                    querySub.add(dictionary.getId());
                }
            }
            querySub.addAll(Arrays.asList(policeType));// 每个节点的所有子节点+自己  用来作参数  *当只有父节点的时候
            map.put(mapKey_policeType, querySub);
        }
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(accessControllerMapper.searchByCondition(map));
    }

    @Override
    public List<ParamInfo> getParamInfoByServiceId(Integer id) {
        return paramInfoMapper.getByServiceId(id);
    }

    @Override
    public List<Keyword> getALLKeyword() {
        return accessControllerMapper.getAllKeyword();
    }

    @Send2Queue
    @Override
    public void addOrSaveACCInfo(AccessController accessController) throws Exception {
        //判断是否存在
        AccessController tmp = accessControllerMapper.getByServiceId(accessController.getServiceId());
        if (tmp == null) {
            accessControllerMapper.save(accessController);
        } else {
            accessControllerMapper.update(accessController);
        }

    }


}
